package com.ygu.Enter2.topic2_2回文序列;

import java.util.Stack;

public class IsPalindromic {

    public boolean isPalindrome(ListNode head) {
        ListNode temp = head;
        //先进后出 后进先出
        Stack<Integer> stack = new Stack<>();

        //入栈操作
        while (temp != null){
            stack.push(temp.val);
            temp= head.next;
        }

        //在一边出栈一边遍历比较
        while (head!=null){
            if(head.val != stack.pop()){
                return false;
            }
            //出栈值和队列的值相同
            head = head.next;
        }
        return true;
    }
    public static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }
}
